package com.wds.controller;

import com.wds.domain.JsonResult;
import com.wds.domain.entity.Category;
import com.wds.domain.vo.CategoryPageVo;
import com.wds.domain.vo.CategoryQueryAllVo;
import com.wds.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author WDs , wds8.24@outlook.com
 * @version 1.0
 * @since 2022-12-31 9:11
 */
@RestController
@RequestMapping("/content/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/listAllCategory")
    public JsonResult<List<CategoryQueryAllVo>> queryAllCategory(){
        return categoryService.queryAllCategory();
    }

    @GetMapping("/list")
    public JsonResult<CategoryPageVo> queryCategoryPage(Integer pageNum, Integer pageSize, String name, Integer status){
        return categoryService.queryCategoryPage(pageNum, pageSize, name, status);

    }

    @PostMapping
    public JsonResult<Object> addNewCategory(@RequestBody Category category){
        return categoryService.addNewCategory(category);
    }

    @DeleteMapping("/{ids}")
    public JsonResult<Object> delete(@PathVariable List<Long> ids){
        return categoryService.delete(ids );
    }

    @GetMapping("/{id}")
    public JsonResult<Object> queryOne(@PathVariable Long id){
        return categoryService.queryOne(id);
    }

    @PutMapping()
    public JsonResult<Object> updateCategory(@RequestBody Category category) {
        return categoryService.updateCategory(category);
    }

    @PreAuthorize("@ps.hasPermission('content:category:export')")
    @GetMapping("/export")
    public void exportExcel(HttpServletResponse response) throws IOException {
        categoryService.exportExcel(response);
    }

}
